const fs = require('fs');
const path = require('path');

module.exports = (projectName) => {
  const projectPath = path.join(__dirname, '../../projects', projectName);

  if (fs.existsSync(projectPath)) {
    throw new Error('项目已存在');
  }
  // 创建项目目录（递归创建）
  if (!fs.existsSync(projectPath)) {
    fs.mkdirSync(projectPath, { recursive: true });
  }

  // 创建images目录（仅在不存在时创建）
  const imagesDir = path.join(projectPath, 'images');
  if (!fs.existsSync(imagesDir)) {
    fs.mkdirSync(imagesDir);
  }

  // 初始化urls.json
  fs.writeFileSync(
    path.join(projectPath, 'urls.json'),
    JSON.stringify([{ index: 0, file: "", url: "" }], null, 2)
  );

  // 创建基础home.pug
  fs.writeFileSync(
    path.join(projectPath, 'home.pug'),
    `extends ../../src/pug/index
block body
  h1 Project ${projectName}
`
  );
};
